package com.chookss.home.tuwen;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.StaticClass;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.Utils;
import com.chookss.tools.XJson;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewsLinkActivity extends BaseAct {

    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.edContent)
    EditText edContent;
    @BindView(R.id.edTitle)
    EditText edTitle;
    @BindView(R.id.rlDelete)
    RelativeLayout rlDelete;
    @BindView(R.id.ivBg)
    View ivBg;
    @BindView(R.id.ivChoice)
    ImageView ivChoice;
    @BindView(R.id.ivChoice2)
    ImageView ivChoice2;
    @BindView(R.id.rlChoice)
    RelativeLayout rlChoice;
    @BindView(R.id.tv_publish)
    TextView tvPublish;
    private String content;
    private int shareFlag = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_link);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        commonTitleTxt.setText("发布");
        String init=new ShareUtils().getString(this, StaticClass.USER_NICKNAME, "")+ " " + Utils.getCurrentTime()+"的新闻";
        edTitle.setText(init);
    }

    private void uploadData() {
        showLoading();
        Map<String, Object> map = new HashMap<>();
        map.put("newsTitle", edTitle.getText().toString());
        if(edContent.getText().toString().startsWith("http")){
            map.put("newsContent",  edContent.getText().toString());
        }else{
            map.put("newsContent", "http://"+edContent.getText().toString());
        }
        map.put("shareFlag", shareFlag);
        map.put("quoteFlag", "2");
        MyHttpRequest.postRequestJson(Urls.addNews, this, XJson.mapToJObj(map).toString(), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        EventBus.getDefault().post(new MyEvent("RefreshNews"));
                        MyToast.show("新闻发布成功");
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                    finish();
                } catch (JSONException e) {
                    e.printStackTrace();
                    dismissLoading();
                    MyToast.show("新闻发布失败，请稍后重试");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (edContent != null) {
            Utils.hideInputKeyboard(this, edContent);
        }
    }


    @OnClick({R.id.common_title_back, R.id.rlDelete, R.id.rlChoice, R.id.tv_publish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.rlDelete:
                edTitle.setText("");
                break;
            case R.id.rlChoice:
                if (ivBg == null) {
                    return;
                }
                if (shareFlag == 1) {
                    shareFlag = 0;
                    ivBg.setVisibility(View.GONE);
                    ivChoice.setVisibility(View.GONE);
                    ivChoice2.setVisibility(View.VISIBLE);
                } else {
                    shareFlag = 1;
                    ivBg.setVisibility(View.VISIBLE);
                    ivChoice.setVisibility(View.VISIBLE);
                    ivChoice2.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_publish:
                if (Utils.isNull(edTitle.getText().toString())) {
                    MyToast.show("请输入标题");
                    return;
                }
                if (Utils.isNull(edContent.getText().toString())) {
                    MyToast.show("链接地址不能为空");
                    return;
                }
                uploadData();
                break;
        }
    }
}
